#include <cstddef>
#include <iostream>
#include "stdafx.h"

#include "projecteventhandler.h"
#include "projectui.h"

ProjectUI::ProjectUI()
{
    facade = NULL;
    mymenu = NULL;
}

ProjectUI::~ProjectUI()
{
    delete mymenu;
}

void ProjectUI::init( IPluginFacade* Cine )
{
    facade = Cine;
}

void ProjectUI::attach( ProjectEventHandler* eventhandler )
{
    std::cerr << "ui attach\n";
    if ( !mymenu )
        mymenu = new CMenu;
    eventhandler->cmd_import = (unsigned int)facade->RegisterCommand( eventhandler );
    eventhandler->cmd_export = (unsigned int)facade->RegisterCommand( eventhandler );
    eventhandler->cmd_build = (unsigned int)facade->RegisterCommand( eventhandler );
    eventhandler->cmd_clean = (unsigned int)facade->RegisterCommand( eventhandler );
    eventhandler->cmd_rebuild = (unsigned int)facade->RegisterCommand( eventhandler );
    eventhandler->cmd_configure = (unsigned int)facade->RegisterCommand( eventhandler );

    mymenu->CreatePopupMenu();
    mymenu->AppendMenu( MF_STRING, eventhandler->cmd_import, TEXT("Import project...") );
    mymenu->AppendMenu( MF_STRING, eventhandler->cmd_export, TEXT("Export project...") );
    mymenu->AppendMenu( MF_STRING, eventhandler->cmd_build, TEXT("&Build") );
    mymenu->AppendMenu( MF_STRING, eventhandler->cmd_clean, TEXT("&Clean") );
    mymenu->AppendMenu( MF_STRING, eventhandler->cmd_rebuild, TEXT("&Rebuild") );
    mymenu->AppendMenu( MF_STRING, eventhandler->cmd_configure, TEXT("Configure...") );
    facade->GetMainMenu().AppendMenu( MF_POPUP, (unsigned int)(mymenu->m_hMenu), TEXT("&Project") );
//     std::cerr << "cmd number: " << eventhandler->cmd_build << ' ' << eventhandler->cmd_clean << ' ' << eventhandler->cmd_rebuild << '\n';
}

void ProjectUI::remove()
{
    mymenu->RemoveMenu( ProjectEventHandler::self()->cmd_import, MF_BYCOMMAND );
    mymenu->RemoveMenu( ProjectEventHandler::self()->cmd_export, MF_BYCOMMAND );
    mymenu->RemoveMenu( ProjectEventHandler::self()->cmd_build, MF_BYCOMMAND );
    mymenu->RemoveMenu( ProjectEventHandler::self()->cmd_clean, MF_BYCOMMAND );
    mymenu->RemoveMenu( ProjectEventHandler::self()->cmd_rebuild, MF_BYCOMMAND );
    mymenu->RemoveMenu( ProjectEventHandler::self()->cmd_configure, MF_BYCOMMAND );
    facade->GetWindowMenu().RemoveMenu( (unsigned int)(mymenu->m_hMenu), MF_BYCOMMAND );
    facade->ReleaseCommand( ProjectEventHandler::self()->cmd_import );
    facade->ReleaseCommand( ProjectEventHandler::self()->cmd_export );
    facade->ReleaseCommand( ProjectEventHandler::self()->cmd_build );
    facade->ReleaseCommand( ProjectEventHandler::self()->cmd_clean );
    facade->ReleaseCommand( ProjectEventHandler::self()->cmd_rebuild );
    facade->ReleaseCommand( ProjectEventHandler::self()->cmd_configure );

    delete mymenu;
    mymenu = NULL;
}

